/**
 * Created by DELL on 2017/5/22.
 */
    var jq=$.noConflict();
    //个人题库控制器
var app = angular.module('appTeacher', ['ngRoute','ngResource','ngCookies'])
    //主页控制器
    .controller('mainCtr',function ($scope,$resource,$cookieStore) {
        $scope.username = $cookieStore.get("loginInfo").name;
        $scope.loginOut = function () {
            $cookieStore.remove('loginInfo');
            window.location.href = '../../login.html';
    }})
    //个人题库控制器
    .controller('quesCtr',function ($scope,callSerive,$resource,$cookieStore) {
    //创建页面提交数据对象
    $scope.userinfo=$cookieStore.get("loginInfo");
    $scope.username=$scope.userinfo.name;
    $scope.postData={
        empid:$scope.userinfo.empid,
        pageindex:1,
        loadtype:0
    };
    $scope.loginOut = function () {
        $cookieStore.remove("loginInfo");
        window.location.href = '../../../login.html';
    };
    $scope.subject=[];
    $scope.datacount=[];
    $scope.indexCount=null;
    $scope.subjectCount=null;
    //按我的资源默认排序
    $scope.getMy=function () {
        $scope.postData.loadtype=0;
        callSerive.setdata($resource,$scope.postData,getData.initial);
    };
    //按最新上传排序
    $scope.getNew=function () {
        $scope.postData.loadtype=1;
        callSerive.setdata($resource,$scope.postData,getData.initial);
    };
    //按浏览数量排序
    $scope.getRead=function () {
        $scope.postData.loadtype=2;
        callSerive.setdata($resource,$scope.postData,getData.initial);
    };
    //按难度排序
    $scope.getDiff=function () {
        $scope.postData.loadtype=3;
        callSerive.setdata($resource,$scope.postData,getData.initial);
    };
    //上一页
    $scope.up=function () {
        $scope.postData.pageindex+=1;
        callSerive.setdata($resource,$scope.postData,getData.initial);
    };
    //下一页
    $scope.down=function () {
        $scope.postData.pageindex-=1;
        callSerive.setdata($resource,$scope.postData,getData.initial);
    };
    //点击分页按钮切换页面
    $scope.paging=function (index) {
        $scope.postData.pageindex=index+1;
        callSerive.setdata($resource,$scope.postData,getData.initial);
    };
    $scope.del=function (id) {
        var deldata={
            Delid:id
        };
        callSerive.delData($resource,deldata,getData.deldata);
    };
    //创建与后台数据交换方法
    var getData= {
        initial:function (res) {
            $scope.datacount=[];
            for(var i=0;i<Math.ceil(res.dataCount/10);i++){
                $scope.datacount.push([i]);
            }
            $scope.subject=[];
            $scope.subjectCount=res.dataCount;
            $scope.subject=res.data;
            $scope.indexCount=Math.ceil($scope.datacount.length/10);
        },
        deldata:function(res){
            callSerive.setdata($resource,$scope.postData,getData.initial);
        }

    };
    callSerive.setdata($resource,$scope.postData,getData.initial);//获取数据

})
    //主页控制器
    .controller("indexCtrl",function ($scope,$resource,$cookieStore) {
    //以下cg开头为页面中（最新，浏览量）等排序字段样式切换预制变量
    $scope.cgcolor=10;
    $scope.cgcolor1=10;
    $scope.cgcolor2=10;
    $scope.cgcolor3=10;
    $scope.bg=null;
    $scope.i=0;
    //分页相关数据
    $scope.loginOut = function () {
        $cookieStore.remove('loginInfo');
        window.location.href = '../../../login.html';
    };
    $scope.pagenum=[];
    $scope.cgIndex=function (n) {//选择选课时，获取数据
        $scope.i=n;
        var postChapterData={
            id:$scope.Allsubject[$scope.i].id,
            CourseName:$scope.Allsubject[$scope.i].courseName,
            parentid:0
        };
        httpServer.getChapter($resource,postChapterData,callService.getchapter);
    };
    //创建获取年级数据发送数据模块，获取cookie
    var postdata={};
    try
    {
        //获取用户信息
        postdata.id=$cookieStore.get('loginInfo').empid;
        $scope.username=$cookieStore.get('loginInfo').name;
        var type=$cookieStore.get('loginInfo').usertype;
        if(type===2) { window.location.href="../../../login.html";}
    }
    catch (e){
        //获取用户信息失败则返回登录页面
        window.location.href="../../../login.html";
    }
    //暂停/播放视频
    $scope.pause=function(mediaid){
        var media=document.getElementById(mediaid);
        media.src = "../../../images/ss.mp4";
    };
    //初始化分页发送数据
    $scope.chapter_id='';
    $scope.knowledge_id='';
    $scope.material_id='';
    $scope.exercises_place='';
    $scope.exercises_type='';
    $scope.exercises_difficlt='';
    $scope.pageindex=0;
    $scope.loadtype='';
    $scope.contentNum='';
    $scope.selectbg = '';
    //章节过滤
    $scope.chapterChange=function (parm) {
        $scope.chapter_id=parm;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //教材过滤
    $scope.materialChange=function (parm,num) {
        $scope.material_id=parm;
        $scope.cgcolor=num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //知识点过滤
    $scope.knowledgeChange = function (parm, num) {
        $scope.knowledge_id=parm;
        $scope.selectbg = num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //场景过滤
    $scope.placeChange=function (parm,num) {
        $scope.exercises_place=parm;
        $scope.cgcolor1=num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //题型过滤
    $scope.typeChange=function (parm,num) {
        $scope.exercises_type=parm;
        $scope.cgcolor2=num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //难点过滤
    $scope.diffChange=function (parm,num) {
        $scope.exercises_difficlt=parm;
        $scope.cgcolor3=num;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //页面索引过滤
    $scope.pageindexChange=function (parm) {
        $scope.pageindex=parm;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //排序过滤
    $scope.loadtypeChange=function (parm) {
        $scope.loadtype=parm;
        $scope.bg=parm;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    //上一页和下一页方法
    $scope.up=function(){
        $scope.pageindex+=1;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };
    $scope.down=function () {
        $scope.pageindex-=1;
        $scope. postConttentData={
            chapter_id:$scope.chapter_id,
            material_id:$scope.material_id,
            knowledge_id:$scope.knowledge_id,
            exercises_place:$scope.exercises_place,
            exercises_type:$scope.exercises_type,
            exercises_difficlt:$scope.exercises_difficlt,
            pageindex:$scope.pageindex,
            loadtype:$scope.loadtype
        };
        httpServer.getData($resource,$scope.postConttentData,callService.getdata);
    };

    //创建后台调用数据接口方法
    var callService={
        getsubject:function (content) {//获取学科数据
            $scope.Allsubject=content.data;
            var postChapterData={
                id:$scope.Allsubject[$scope.i].id,
                CourseName:$scope.Allsubject[$scope.i].courseName,
                parentid:0
            };
            httpServer.getChapter($resource,postChapterData,callService.getchapter);
        },
        getchapter:function (content) {//获取目录数据
            $scope.parentChapter=content.data;
            var postPointData={
                gradeid:$scope.Allsubject[$scope.i].id,
                CourseName: $scope.Allsubject[$scope.i].courseName,
                parentid:0
            };
            httpServer.getPoint($resource,postPointData,callService.getpoint);
        },
        getpoint:function(content){//获取知识点数据
            $scope.point=content.data;
            var postMaterialData={
                CourseName:$scope.Allsubject[$scope.i].courseName,
                id: $scope.Allsubject[$scope.i].id,
                parentid: 0
            };
            httpServer.getMaterial($resource,postMaterialData,callService.getmaterial);
        },
        getmaterial:function (content) {//获取教材数据
            $scope.Material=content.data;
            httpServer.getPlace($resource,callService.getplace);
        },
        getplace:function (content) {//获取场景数据
            $scope.place=content.data;
            httpServer.getType($resource,callService.gettype);
        },
        gettype:function (content) {//获取类型数据
            $scope.type=content.data;
            httpServer.getDiff($resource,callService.getdiff);
        },
        getdiff:function (content) {//获取难度数据
            $scope.diff=content.data;
            httpServer.getData($resource,$scope.postConttentData,callService.getdata);
        },
        getdata:function (content) {
            $scope.contentNum=content;
            $scope.contentData=content.data;
            var length=Math.ceil(content.dataCount/10);
            $scope.pagenum=[];
            for(var i=0;i<length;i++){
                $scope.pagenum.push([i]);
            }
        }
    };

    //初始化数据
    httpServer.getSubject($resource,postdata,callService.getsubject);
})
    //个人设置页面控制器
    .controller("setCtr", function ($scope, $resource, $cookieStore) {
        $scope.username = $cookieStore.get("loginInfo").name;
        $scope.loginOut = function () {
            $cookieStore.remove('loginInfo');
            window.location.href = '../../login.html';
        }
    })
    //密码修改页面控制器
    .controller("regCtr", function ($scope, $resource, $cookieStore) {
        $scope.username = $cookieStore.get("loginInfo").name;
        $scope.loginOut = function () {
            $cookieStore.remove('loginInfo');
            window.location.href = '../../login.html';
        };
        $scope.postdata = {
            oldPsd: '',
            newPsd: '',
            newPsdRe: ''
        };
        var service = {
            getmsg: function (content) {
                if (content.isSuccess && content.dataCount == 0) {
                    jq("#regErr").css({"color": "red"});
                    jq("#regback img").css({"visibility": "visible"});
                    $scope.errmsg = "旧密码错误";
                }
                else if (content.isSuccess && content.dataCount > 0) {
                    jq("#regback img").css({"visibility": "hidden"});
                    jq("#regErr").css({"color": "green"});
                    $scope.errmsg = content.message;
                }
                else {
                    jq("#regErr").css({"color": "red"});
                    jq("#regback img").css({"visibility": "visible"});
                    $scope.errmsg = "修改异常，刷新页面试试吧！";
                }
            }
        };
        $scope.errmsg = '';
        $scope.modify = function () {
            if ($scope.postdata.oldPsd == '' || $scope.postdata.newPsd == '' || $scope.postdata.newPsdRe == '') {
                jq("#regback img").css({"visibility": "visible"});
                $scope.errmsg = "新旧密码及密码确认不能为空";
            }
            else if ($scope.postdata.oldPsd != '' && $scope.postdata.oldPsd == $scope.postdata.newPsd) {
                jq("#regback img").css({"visibility": "visible"});
                $scope.errmsg = "新旧密码不能相同！";
            }
            else if ($scope.postdata.newPsd != '' && $scope.postdata.newPsd != $scope.postdata.newPsdRe) {
                jq("#regback img").css({"visibility": "visible"});
                $scope.errmsg = "新密码和确认密码不一致!";
            }
            else {
                var sentdata = {
                    loginname: $cookieStore.get("loginInfo").username,
                    oldpasswords: $scope.postdata.oldPsd,
                    newpasswords: $scope.postdata.newPsd
                };
                httpServer.modifyPsd($resource, sentdata, service.getmsg);
            }
        }
    })
    //学生错题集控制器
    .controller("errCtr", function ($resource, $cookieStore, $scope) {
        //获取用户信息
        $scope.username = $cookieStore.get("loginInfo").name;
        $scope.loginOut = function () {
            $cookieStore.remove('loginInfo');
            window.location.href = '../../login.html';
        };
        //创建学生参数
        $scope.stuInfo = '';
        $scope.name = "chapter";
        //创建章节参数
        $scope.chapterInfo = '';
        //创建后台数据接口方法、
        var dataService = {
            //获取学生信息
            getStuData: function (content) {
                $scope.stuInfo = content.data;
                httpServer.getErrChapter($resource, dataService.getChapters);
            },
            //获取章节信息
            getChapters: function (content) {
                $scope.chapterInfo = content.data;
            }
        };
        httpServer.getErrStu($resource, dataService.getStuData);
    });

